package com.joke.mydiary;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.joke.mydiary.DaoImpl.DiaryDao;
import com.joke.mydiary.fragment.AddDiaryFragment;
import com.joke.mydiary.fragment.ListDiaryFragment;
import com.joke.mydiary.pojo.Diary;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements AddDiaryFragment.AddDiaryFragmentListener, ListDiaryFragment.ListDiaryListener {

    private DiaryDao diaryDao = new DiaryDao(this);
    private FragmentManager fm;
    private ListDiaryFragment ldf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ldf = new ListDiaryFragment();
        ft.replace(R.id.mainLayout, ldf);
        ft.commit();

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (ldf != null && !newText.equals("")) {
                    Cursor cursor = diaryDao.findByKeyCursor(newText);
                    ldf.searchData(cursor);
                } else {
                    Cursor cursor = diaryDao.findAllCursor();
                    ldf.searchData(cursor);
                }
                return false;
            }
        });
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent=new Intent(this,SettingActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    //保存备忘录
    @Override
    public boolean saveDiary(Diary diary) {
        if (diary != null) {
            return diaryDao.insert(diary);
        } else return false;
    }

    @Override
    public DiaryDao getDiaryDao() {
        return diaryDao;
    }

    @Override
    public void showAddDiaryFragment() {
        fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.mainLayout, new AddDiaryFragment());
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public boolean deleteByMenu(String time) {

        boolean flag = diaryDao.deleteByTime(time);
        return flag;
    }

    //用于云备份操作
    @Override
    public void startBackupService(Intent intent) {
        startService(intent);
    }

    @Override
    public void showListDiaryFramgment() {
        fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ListDiaryFragment ldf = new ListDiaryFragment();
        ft.replace(R.id.mainLayout, ldf);
        ft.commit();
    }

    @Override
    public InputStream getUriInput(Uri uri) {
        try {
            return getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(getFragmentManager().getBackStackEntryCount()==0)
            {
                finish();
            }else
            {
                getFragmentManager().popBackStack();
            }
        }
        return true;
    }
}
